var e = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../@babel/runtime/regenerator")), t = require("../../../@babel/runtime/helpers/asyncToGenerator"), a = getApp(), r = require("../../../poster/poster.js");

Component({
    behaviors: [ r ],
    properties: {
        store_alone_temp: {
            type: null
        },
        activity_id: {
            type: null
        },
        new_poster_id: {
            type: null
        }
    },
    data: {
        poster_id: 0,
        step: 1,
        is_new: 0
    },
    ready: function() {},
    methods: {
        initPosterData: function() {
            var a = this;
            return t(e.default.mark(function t() {
                var r, s, n;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.next = 2, a.getMyCanvasMessage(640, 1008);

                      case 2:
                        r = e.sent, a.setData({
                            canvasWidth: r.width,
                            canvasHeight: r.height
                        }), a.data.defaultImg ? (a.setData({
                            loading: 100,
                            poster: a.data.defaultImg
                        }), a.triggerEvent("finishDefaultImg", a.data.poster)) : (s = a.data.posterData, 
                        n = {
                            background: s.background.src,
                            type: s.background.type,
                            style_data: s.poster_data
                        }, console.log(n, "jsonarr"), a.startDrawCanvas(r, n));

                      case 5:
                      case "end":
                        return e.stop();
                    }
                }, t);
            }))();
        },
        getPosterPic: function() {
            var r = this;
            return t(e.default.mark(function t() {
                var s, n;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.next = 2, r.getMyCanvasMessage(640, 1008);

                      case 2:
                        s = e.sent, r.setData({
                            canvasWidth: s.width,
                            canvasHeight: s.height
                        }), n = a.getNetAddresss("member.qrcode.get-poster"), 1 == r.data.store_alone_temp && (n = a.getNetAddresss("plugin.store-alone-temp.frontend.qrcode.get-poster")), 
                        r.data.poster_id && (n = n + "&poster_id=" + r.data.poster_id), r.data.new_poster_id && r.data.activity_id && (n = (n = a.getNetAddresss("plugin.crowdfunding.frontend.activity-member.promotePoster")) + "&new_poster_id=" + r.data.new_poster_id + "&activity_member_id=" + r.data.activity_id), 
                        a._getNetWork({
                            showToastIn: !1,
                            url: n,
                            success: function(e) {
                                var t = e.data;
                                if (1 == t.result) if (r.data.new_poster_id && r.data.activity_id) if (t.data.new) {
                                    var a = {
                                        background: t.data.background.src,
                                        type: t.data.background.type,
                                        style_data: t.data.style_data
                                    };
                                    r.startDrawCanvas(s, a);
                                } else r.setData({
                                    loading: 100,
                                    poster: t.data
                                }); else 0 == t.data.center_show ? r.setData({
                                    loading: 100,
                                    poster: t.data.image_url
                                }) : 1 == t.data.center_show ? (r.data.poster_id = t.data.id, t.data.new && 1 == t.data.new ? (r.data.is_new = 1, 
                                r.startDrawCanvas(s, t.data)) : r.startOldDrawCanvas(s, t.data)) : 2 == t.data.center_show && r.getPosterTp2();
                            }
                        });

                      case 9:
                      case "end":
                        return e.stop();
                    }
                }, t);
            }))();
        },
        getPosterTp2: function() {
            var e = this, t = this, r = a.getNetAddresss("member.poster");
            a._getNetWork({
                url: r,
                success: function(a) {
                    var r = a.data;
                    1 == r.result ? "" == r.data.image_url || null == r.data.image_url ? (wx.showToast({
                        title: "请稍等" + r.data.image_url,
                        icon: "none",
                        duration: 2e3
                    }), setTimeout(function() {
                        e.getPosterTp2();
                    }, 500)) : "" != r.data.image_url && setTimeout(function() {
                        t.setData({
                            poster: r.data.image_url
                        });
                    }, 2e3) : wx.showToast({
                        title: r.msg,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(e) {
                    this.data.timer && clearInterval(this.data.timer), wx.hideLoading({});
                }
            });
        },
        startDrawCanvas: function(a, r) {
            var s = this;
            return t(e.default.mark(function n() {
                var i, o, d, c, u, p, g;
                return e.default.wrap(function(n) {
                    for (;;) switch (n.prev = n.next) {
                      case 0:
                        if ("posterCenter" == s.data.plugin_name && wx.showLoading({
                            title: "海报加载中"
                        }), (i = wx.createCanvasContext("myCanvas", s)).save(), "color" == r.type ? i.setFillStyle(r.background) : i.setFillStyle("#fff"), 
                        s.drawRoundRectPath(i, a.width, a.height, 30), i.fillRect(0, 0, a.width, a.height), 
                        "color" == r.type) {
                            n.next = 12;
                            break;
                        }
                        return n.next = 9, s.downImage({
                            src: r.background
                        });

                      case 9:
                        o = n.sent, s.setProportion(a, o), s.drawCanvasImage(i, {
                            left: 0,
                            top: 0,
                            width: o.width / s.data.step,
                            height: o.height / s.data.step,
                            img: o.img
                        });

                      case 12:
                        s.setData({
                            loading: 10
                        }), d = r.style_data, c = 0;

                      case 15:
                        if (!(c < d.length)) {
                            n.next = 33;
                            break;
                        }
                        if ("nickname" != (u = d[c]).type && "invite" != u.type && "text" != u.type && "mid" != u.type) {
                            n.next = 21;
                            break;
                        }
                        s.drawCanvasText(i, u), n.next = 28;
                        break;

                      case 21:
                        if ("img" != u.type && "head" != u.type && "qr" != u.type && "shopqr" != u.type && "logo" != u.type) {
                            n.next = 28;
                            break;
                        }
                        if (!u.src) {
                            n.next = 28;
                            break;
                        }
                        return n.next = 25, s.downImage(u);

                      case 25:
                        p = n.sent, u.img = p.img, s.drawCanvasImage(i, u);

                      case 28:
                        g = parseInt(80 / d.length * (c + 1)), s.setData({
                            loading: g + 10
                        });

                      case 30:
                        c++, n.next = 15;
                        break;

                      case 33:
                        i.draw(!1, setTimeout(t(e.default.mark(function t() {
                            var r;
                            return e.default.wrap(function(e) {
                                for (;;) switch (e.prev = e.next) {
                                  case 0:
                                    return e.next = 2, s.toimage("myCanvas", a);

                                  case 2:
                                    if (r = e.sent, s.setData({
                                        loading: 100,
                                        poster: r
                                    }), "posterCenter" != s.data.plugin_name) {
                                        e.next = 10;
                                        break;
                                    }
                                    return wx.hideLoading({
                                        success: function(e) {}
                                    }), s.triggerEvent("finishDefaultImg", s.data.poster), e.abrupt("return");

                                  case 10:
                                    s.upQrcodeImg(r);

                                  case 11:
                                  case "end":
                                    return e.stop();
                                }
                            }, t);
                        })), 1e3));

                      case 34:
                      case "end":
                        return n.stop();
                    }
                }, n);
            }))();
        },
        startOldDrawCanvas: function(a, r) {
            var s = this;
            return t(e.default.mark(function n() {
                var i, o, d, c, u, p, g, l;
                return e.default.wrap(function(n) {
                    for (;;) switch (n.prev = n.next) {
                      case 0:
                        return (i = wx.createCanvasContext("myCanvas", s)).save(), s.drawRoundRectPath(i, a.width, a.height, 30), 
                        i.setFillStyle("#fff"), i.fillRect(0, 0, a.width, a.height), n.next = 7, s.downImage({
                            src: r.background
                        });

                      case 7:
                        o = n.sent, s.setProportion(a, o), s.drawCanvasImage(i, {
                            left: 0,
                            top: 0,
                            width: o.width / s.data.step,
                            height: o.height / s.data.step,
                            img: o.img
                        }), s.setData({
                            loading: 10
                        }), d = r.style_data, c = 0;

                      case 13:
                        if (!(c < d.length)) {
                            n.next = 39;
                            break;
                        }
                        if ("nickname" != (u = d[c]).type) {
                            n.next = 19;
                            break;
                        }
                        s.drawCanvasText(i, u), n.next = 34;
                        break;

                      case 19:
                        if ("img" != u.type && "head" != u.type) {
                            n.next = 28;
                            break;
                        }
                        if (!u.src) {
                            n.next = 26;
                            break;
                        }
                        return n.next = 23, s.downImage(u);

                      case 23:
                        p = n.sent, u.img = p.img, s.drawCanvasImage(i, u);

                      case 26:
                        n.next = 34;
                        break;

                      case 28:
                        if ("qr_shop" != u.type && "qr_app_share" != u.type) {
                            n.next = 34;
                            break;
                        }
                        return n.next = 31, s.getQRCodeUrl(u);

                      case 31:
                        g = n.sent, u.img = g, s.drawCanvasImage(i, u);

                      case 34:
                        l = parseInt(80 / d.length * (c + 1)), s.setData({
                            loading: l + 10
                        });

                      case 36:
                        c++, n.next = 13;
                        break;

                      case 39:
                        i.draw(!1, setTimeout(t(e.default.mark(function t() {
                            var r;
                            return e.default.wrap(function(e) {
                                for (;;) switch (e.prev = e.next) {
                                  case 0:
                                    return e.next = 2, s.toimage("myCanvas", a);

                                  case 2:
                                    r = e.sent, s.setData({
                                        loading: 100,
                                        poster: r
                                    }), s.upQrcodeImg(r);

                                  case 5:
                                  case "end":
                                    return e.stop();
                                }
                            }, t);
                        })), 1e3));

                      case 40:
                      case "end":
                        return n.stop();
                    }
                }, n);
            }))();
        },
        upQrcodeImg: function(e) {
            var t = this, r = wx.getStorageSync("wx_token"), s = wx.getStorageSync("session"), n = a.getNetAddresss("member.qrcode.uploadLocal");
            n += "&poster_id=" + this.data.poster_id + "&is_new=" + this.data.is_new, n += "&3rd_session=" + s, 
            wx.uploadFile({
                url: n,
                filePath: e,
                name: "file",
                formData: {
                    _wx_token: wx.getStorageSync("wx_token"),
                    ingress: 0
                },
                header: {
                    "Content-Type": "multipart/form-data",
                    Cookie: "PHPSESSID=" + r
                },
                success: function(e) {
                    var a = JSON.parse(e.data);
                    1 == a.result && t.addPosterRecord(a.data.img_url);
                },
                fail: function(e) {}
            });
        },
        addPosterRecord: function(e) {
            var t = a.getNetAddresss("member.qrcode.poster-record");
            a._getNetWork({
                showToastIn: !1,
                url: t,
                data: {
                    image: e,
                    poster_id: this.data.poster_id,
                    is_new: this.data.is_new
                },
                success: function(e) {}
            });
        }
    }
});